/*Created under the GNU General Public Liseance Project:Sinhala Synonyms Supporter Author S.N.S.Dewasurendra
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wordSelect;

/**
 *
 * @author User
 */
import com.sun.star.beans.XPropertySet;
import com.sun.star.frame.XController;
import com.sun.star.frame.XDesktop;
import com.sun.star.frame.XModel;
import com.sun.star.lang.EventObject;
import com.sun.star.lang.XComponent;
import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.text.XPageCursor;
import com.sun.star.text.XText;
import com.sun.star.text.XTextCursor;
import com.sun.star.text.XTextViewCursor;
import com.sun.star.text.XTextViewCursorSupplier;
import com.sun.star.text.XWordCursor;
import com.sun.star.uno.Exception;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;
import com.sun.star.lang.XEventListener;

public class wordSelection implements XEventListener {

    private String word;
    private final XComponentContext m_xContext;

    public wordSelection(XComponentContext m_xContext) {
        this.m_xContext = m_xContext;
        word = "";

    }

    public String doSelect() {

        XMultiComponentFactory xmcf = m_xContext.getServiceManager();
        Object desktop;


        try {
            desktop = xmcf.createInstanceWithContext("com.sun.star.frame.Desktop", m_xContext);
            XDesktop xDesktop = (XDesktop) UnoRuntime.queryInterface(XDesktop.class, desktop);


            // retrieve the current component and access the controller
            XComponent xCurrentComponent = xDesktop.getCurrentComponent();


            // get the XModel interface from the component
            XModel xModel = (XModel) UnoRuntime.queryInterface(XModel.class, xCurrentComponent);


            // the model knows its controller
            XController xController = xModel.getCurrentController();


            XTextViewCursorSupplier xViewCursorSupplier =
                    (XTextViewCursorSupplier) UnoRuntime.queryInterface(
                    XTextViewCursorSupplier.class, xController);


            // get the cursor
            XTextViewCursor xViewCursor = xViewCursorSupplier.getViewCursor();


            // query its XPropertySet interface, we want to set character and paragraph properties
            XPropertySet xCursorPropertySet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xViewCursor);




            XPageCursor xPageCursor = (XPageCursor) UnoRuntime.queryInterface(
                    XPageCursor.class, xViewCursor);
            XText xDocumentText = xViewCursor.getText();
            XTextCursor xModelCursor = xDocumentText.createTextCursorByRange(xViewCursor.getStart());

            XWordCursor xWordCurser = (XWordCursor) UnoRuntime.queryInterface(
                    XWordCursor.class, xModelCursor);


            xWordCurser.gotoPreviousWord(true);

            String lastWord = xWordCurser.getString();
            System.out.println(lastWord + "sdgfdshgfhd");


            if (lastWord.endsWith(" ")) {
                System.out.println("matzzzz");
                xWordCurser.gotoEndOfWord(true);
                xWordCurser.gotoEndOfWord(true);
                word = xWordCurser.getString().replaceAll("[^A-Za-z]", "");
                System.out.println(word);

            } else {
                xWordCurser.gotoEndOfWord(true);
                word = (lastWord + xWordCurser.getString()).replaceAll("[^A-Za-z]", "");
            }
            new wordTranslator().setWord(word);


            return word;

        } catch (Exception e) {
            System.out.print(e);
            e.printStackTrace();
            return null;

        }
    }

//    public void AddKeyListener() {
//
//        XMultiComponentFactory xmcf = m_xContext.getServiceManager();
//        Object desktop;
//
//        try {
//            desktop = xmcf.createInstanceWithContext("com.sun.star.frame.Desktop", m_xContext);
//            XDesktop xDesktop = (XDesktop) UnoRuntime.queryInterface(XDesktop.class, desktop);
//            XWindow MyWindow = xDesktop.getCurrentFrame().getComponentWindow(); //for testing
//
//            XWindowPeer MyWindowPeer = (XWindowPeer) UnoRuntime.queryInterface(XWindowPeer.class, MyWindow);
//            XToolkit MyToolkit = MyWindowPeer.getToolkit();
//            XExtendedToolkit MyExtToolkit = (XExtendedToolkit) UnoRuntime.queryInterface(XExtendedToolkit.class, MyToolkit);
//            MyExtToolkit.addKeyHandler((XKeyHandler) this);
//
//
//        } catch (Exception e) {
//            System.out.println("dispatch Exception : " + e.getMessage());
//            e.printStackTrace();
//        }
//
//
//    }
    public void disposing(EventObject arg0) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
